﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Reflection;
using GalaSoft.MvvmLight.Messaging;

namespace ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.Views
{
    public partial class CustomTypePage : ViewPageBase
    {
        public ObservableCollection<Customer> customers = new ObservableCollection<Customer>();

        public CustomTypePage()
        {
            InitializeComponent();

            customersGrid.ItemsSource = customers;
            Messenger.Default.Register<MessageType>(this, ManageMessage);
        }

        static CustomTypePage()
        {
            Customer.AddProperty("Age", typeof(int));
            Customer.AddProperty("Married", typeof(bool));
            Customer.AddProperty("Nickname", typeof(string));
        }

        private void ManageMessage(MessageType messageType)
        {
            switch (messageType)
            {
                case MessageType.LoadCustomType:
                    Load();
                    break;
            }
        }
        private void Load()
        {
            Customer customer1 = new Customer { FirstName = "Mary", LastName = "Smith" };
            customer1.SetPropertyValue("Age", 40);
            customer1.SetPropertyValue("Married", true);
            customer1.SetPropertyValue("Nickname", "mary-4ever");

            Customer customer2 = new Customer { FirstName = "John", LastName = "Smith" };
            customer2.SetPropertyValue("Age", 45);
            customer2.SetPropertyValue("Married", true);

            customers.Add(customer1);
            customers.Add(customer2);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }

    public class Customer : ICustomTypeProvider
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }

        CustomTypeHelper<Customer> helper = new CustomTypeHelper<Customer>();

        public static void AddProperty(String name)
        {
            CustomTypeHelper<Customer>.AddProperty(name);
        }

        public static void AddProperty(String name, Type propertyType)
        {
            CustomTypeHelper<Customer>.AddProperty(name, propertyType);
        }

        public static void AddProperty(String name, Type propertyType, List<Attribute> attributes)
        {
            CustomTypeHelper<Customer>.AddProperty(name, propertyType, attributes);
        }

        public void SetPropertyValue(string propertyName, object value)
        {
            helper.SetPropertyValue(propertyName, value);
        }

        public object GetPropertyValue(string propertyName)
        {
            return helper.GetPropertyValue(propertyName);
        }

        public PropertyInfo[] GetProperties()
        {
            return helper.GetProperties();
        }

        public Type GetCustomType()
        {
            return helper.GetCustomType();
        }
    }

}
